<template>
  <el-row>
    <!-- 标题 -->
    <el-col :span="24" class="sub-title-text">
      <span>{{ paramTitle }}</span>
    </el-col>
    <!-- 按钮组 -->
    <el-col :span="24">
      <el-radio-group v-model="selectedAlgorithm">
        <el-row>
          <el-col v-for="obj in radioObjects" :key="obj.name" :span="obj.span">
            <el-radio :label="obj.name">{{ obj.name }}</el-radio>
          </el-col>
        </el-row>
      </el-radio-group>
    </el-col>
    <!-- 面板细节 -->
    <el-col :span="24">
      <el-row>
        <slot> </slot>
      </el-row>
    </el-col>
  </el-row>
</template>

<script>
import { ElMessage } from 'element-plus';
export default {
  props: {
    paramTitle: String, //标题
    defaultSelectedAlgorithm: String, //默认选择的算法
    radioObjects: Array, //按钮组对象
  },
  data() {
    return {
      selectedAlgorithm: null,
    };
  },
  watch: {
    selectedAlgorithm: {
      //将选择的算法标识发送给父组件
      handler() {
        this.$emit('selectRadio', this.selectedAlgorithm);
      },
    },
  },
  mounted() {
    this.selectedAlgorithm = this.defaultSelectedAlgorithm; //默认选择的算法
  },
  methods: {
    async executeAlgorithm(formRef, formData, selectedAlgorithm) {
      try {
        await formRef.validate(); //校验表单
        const algorithmName = selectedAlgorithm; //算法名称获取
        const algorithmObject = new AlgorithmObject(algorithmName); //构建算法对象
        //从formData中找到以算法名开头的key
        for (let key in formData) {
          if (key.startsWith(algorithmName)) {
            algorithmObject.pushParam(key, this.formData[key]); //添加参数
          }
        }
        console.log(algorithmObject);
      } catch (err) {
        // 校验失败
        for (let key in err) {
          const errMessage = err[key][0]['message'];
          ElMessage.error(errMessage);
        }
      }
    },
  },
};
</script>
<style lang="scss" scoped>
// 单选框组合样式修改
.el-radio-group {
  display: flex;
  .el-row {
    width: 100%;
    .el-radio {
      :deep(.el-radio__label) {
        font-size: $middle-font-size;
        font-weight: bold;
      }
    }
  }
}
.el-col {
  margin: 2px 0;
}
</style>
